using System.Collections.ObjectModel;
using System.Windows.Input;
using Avalonia.Threading;
using CommunityToolkit.Mvvm.Input;
using Klear.Library.Models;
using Klear.Library.Services;

namespace Klear.Library.ViewModels;

public class BudgetListViewModel : ViewModelBase
{
    private readonly IBudgetService _budgetService;
    private ObservableCollection<BudgetModel> _budgets;
    private BudgetModel _selectedBudget;
    private bool _isLoading;
    private bool _isRefreshing;

    
    public BudgetListViewModel(IBudgetService budgetService)
    {
        _budgetService = budgetService?? throw new ArgumentNullException(nameof(budgetService));
        _budgets = new ObservableCollection<BudgetModel>();
        
        LoadBudgetsCommand = new AsyncRelayCommand(LoadBudgets);
        RefreshBudgetsCommand = new AsyncRelayCommand(RefreshBudgets);
        DeleteBudgetCommand = new AsyncRelayCommand(DeleteBudget);

        _ = InitializeAsync();
    }
    
    private async Task InitializeAsync()
    {
        try
        {
            IsLoading = true;
            await LoadBudgets();
        }
        finally
        {
            IsLoading = false;
        }
    }

    public ICommand LoadBudgetsCommand { get; }
    public ICommand RefreshBudgetsCommand { get; }
    public ICommand DeleteBudgetCommand { get; }
    
    public ObservableCollection<BudgetModel> Budgets
    {
        get => _budgets;
        set
        {
            if (_budgets != value)
            {
                _budgets = value;
                OnPropertyChanged(nameof(Budgets));
            }
        }
    }
    
    public BudgetModel SelectedBudget
    {
        get => _selectedBudget;
        set
        {
            if (_selectedBudget != value)
            {
                _selectedBudget = value;
                OnPropertyChanged(nameof(SelectedBudget));
            }
        }
    }
    
    public bool IsLoading
    {
        get => _isLoading;
        private set => SetProperty(ref _isLoading, value);
    }
    
    public bool IsRefreshing
    {
        get => _isRefreshing;
        set
        {
            if (_isRefreshing != value)
            {
                _isRefreshing = value;
                OnPropertyChanged(nameof(IsRefreshing));
            }
        }
    }

    private async Task LoadBudgets()
    {
        if (IsLoading) return;
        try
        {
            IsLoading = true;
            
            var budgetList = await _budgetService.GetCurrentAndFutureBudgetsAsync();
            

            if (budgetList == null || !budgetList.Any())
            {
                Budgets.Clear();
                return;
            }
            
            Budgets.Clear();
            foreach (var budget in budgetList)
            {
                Budgets.Add(budget);
            }
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine(ex.StackTrace);
        }
        finally
        {
            IsLoading = false;
        }
    }
    
    
    private async Task RefreshBudgets()
    {
        if (IsRefreshing) return;
        IsRefreshing = true;
        await LoadBudgets();
        IsRefreshing = false;
    }
    

    private async Task DeleteBudget()
    {
        try
        {
            await _budgetService.DeleteBudgetAsync(SelectedBudget);
            Budgets.Remove(SelectedBudget);
            SelectedBudget = null;
        }
        catch (Exception ex)
        {
            System.Diagnostics.Debug.WriteLine($"删除预算数据失败: {ex.Message}");
        }
    }
}